package com.boot.unity.api.wx.service;

import com.alibaba.fastjson.JSONObject;
import com.boot.unity.api.constants.NotifyType;
import com.boot.unity.api.utils.ConstantSmsUtils;
import com.boot.unity.api.wx.utils.CharUtil;
import com.boot.unity.api.wx.utils.IpAddress;
import com.boot.unity.api.wx.utils.ResponseUtil;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class WxBindMangerService {

    @Autowired
    private ConstantSmsUtils constantSmsUtils;

    @Autowired
    private CommonUtilUrlService commonUtilUrlService;

    @Value("${serv.servurl}")
    private String servurl;


    public Object checkUser(String id) {
        OkHttpClient client = new OkHttpClient();
        String url = servurl+ "/wx/bindManager/checkUser?id=" + id;
        Request request = new Request.Builder()
                .url(url)
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }
    }

    public Object clearBind(String userid) {
        OkHttpClient client = new OkHttpClient();
        String url = servurl+ "/wx/bindManager/clearBind?userid=" + userid;
        Request request = new Request.Builder()
                .url(url)
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }
    }

    public Object schoolDetail(String id, String key) {
        OkHttpClient client = new OkHttpClient();
        String url = servurl+ "/wx/bindManager/schoolDetail?id=" + id+"&key=" + key;
        Request request = new Request.Builder()
                .url(url)
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }
    }

    public Object getCode(String phoneNumber) throws Exception {
        String code = CharUtil.getRandomNum(6);
        System.out.println("-----生成的验证码："+code);
        JSONObject paramJson = new JSONObject();
        paramJson.put("phoneNumber",phoneNumber);
        paramJson.put("code",code);
        String msgUrl = servurl+"/wx/bindManager/checkOrSaveMsg";
        System.out.println("请求参数："+paramJson.toString());
        String result = commonUtilUrlService.post(msgUrl, paramJson.toString());
        System.out.println("请求结果："+result.toString());
        if(!result.trim().equals("1")){
            return ResponseUtil.fail(701, "验证码未超时1分钟，不能发送");
        }else{
            // 发送短信
            constantSmsUtils.send(phoneNumber, NotifyType.CAPTCHA, new String[]{code});
            return ResponseUtil.ok();
        }
        /*String code = CharUtil.getRandomNum(6);
        //设置一分钟不允许重复发送
        boolean successful = CaptchaCodeManager.addToCache(mobile, code);
        if (!successful) {
            return ResponseUtil.fail(701, "验证码未超时1分钟，不能发送");
        }
        //发送短信
        constantSmsUtils.send(mobile, NotifyType.CAPTCHA, new String[]{code});

        OkHttpClient client = new OkHttpClient();
        String url = servurl+ "/wx/bindManager/getCode?mobile=" + mobile+"&code="+code;
        Request request = new Request.Builder()
                .url(url)
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }*/
    }

    public Object scanBindManager(String id, String manangerid) {
        OkHttpClient client = new OkHttpClient();
        String url = servurl+ "/wx/bindManager/scanBindManager?id=" + id+"&manangerid=" + manangerid;
        Request request = new Request.Builder()
                .url(url)
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }
    }

    public Object bind(String id, String name, String mobile, String cardNo, String persole, String code) {
        OkHttpClient client = new OkHttpClient();
        String url = servurl+ "/wx/bindManager/scanBindManager?id=" + id+"&name=" + name+"&mobile=" + mobile+"&cardNo=" + cardNo+"&persole="+persole+"&code="+code;
        Request request = new Request.Builder()
                .url(url)
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }
    }

    public Object mangerIdData(String id, String key) {
        OkHttpClient client = new OkHttpClient();
        String url = servurl+ "/wx/bindManager/mangerIdData?id=" + id+"&key=" + key;
        Request request = new Request.Builder()
                .url(url)
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }
    }

    public Object verification(String id, String name, String mobile, String cardNo) {
        OkHttpClient client = new OkHttpClient();
        String url = servurl+ "/wx/bindManager/verification?id=" + id+"&name=" + name+"&mobile=" + mobile+"&cardNo=" + cardNo;
        Request request = new Request.Builder()
                .url(url)
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }
    }
}
